package com.tenbent.product.center.insures.action;

import com.tenbent.product.base.action.BaseController;
import com.tenbent.product.base.constants.WebConstans;
import com.tenbent.product.base.query.page.PageResult;
import com.tenbent.product.base.web.Page;
import com.tenbent.product.center.insures.bo.SafeguardCategoryType;
import com.tenbent.product.center.insures.dto.SafeguardCategoryTypeDto;
import com.tenbent.product.center.insures.dto.search.SafeguardCategoryTypePageDto;
import com.tenbent.product.center.insures.service.SafeguardCategoryTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 产品保障大分类控制器
 *
 * @author Randy
 *
 *         Created by ThinkPad on 2017/8/28.
 */
@RestController
public class SafeguardCategoryTypeController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SafeguardCategoryTypeController.class);

	public static class Url {
		/** 添加产品 */
		public static final String save = "/api/safeguardCategoryType/save";
		/** 修改产品 */
		public static final String update = "/api/safeguardCategoryType/update";
		/** 删除产品 */
		public static final String delete = "/api/safeguardCategoryType/delete/{id}";
		/** 根据产品ID查询 */
		public static final String queryByProductId = "/api/safeguardCategoryType/query_by_product_id";
		/** 分页查询 */
		public static final String query = "/api/safeguardCategoryType/query";

	}

	@Autowired
	@Qualifier("safeguardCategoryTypeServiceImpl")
	private SafeguardCategoryTypeService safeguardCategoryTypeService;

	@RequestMapping(value = Url.save, method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseStatus(HttpStatus.CREATED)
	public SafeguardCategoryTypeDto saveApi(@Valid @RequestBody SafeguardCategoryTypeDto safeguardCategoryTypeDto,
			Errors errors, @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		processErrors(errors);
		settingSaveManager(safeguardCategoryTypeDto, managerId);
		SafeguardCategoryType safeguardCategoryType = safeguardCategoryTypeDto.transToBo();
		safeguardCategoryTypeService.add(safeguardCategoryType);
		return new SafeguardCategoryTypeDto(safeguardCategoryType);
	}

	@RequestMapping(value = Url.update, method = {
			RequestMethod.PUT }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SafeguardCategoryTypeDto updateApi(@Valid @RequestBody SafeguardCategoryTypeDto safeguardCategoryTypeDto,
			Errors errors, @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		if (safeguardCategoryTypeDto.getId() == null) {
			errors.rejectValue("id", "safeguard.category.type.id.not.empty",
					getMessage("safeguard.category.type.id.not.empty", null));
		}
		processErrors(errors);
		settingModiferManager(safeguardCategoryTypeDto, managerId);
		SafeguardCategoryType safeguardCategoryType = safeguardCategoryTypeDto.transToBo();
		safeguardCategoryTypeService.update(safeguardCategoryType);
		return new SafeguardCategoryTypeDto(safeguardCategoryType);
	}

	@RequestMapping(value = Url.delete, method = {
			RequestMethod.DELETE }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void deleteApi(@PathVariable Integer id, @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {
		safeguardCategoryTypeService.deleteLogic(id, managerId);
	}

	@RequestMapping(value = Url.query, method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<SafeguardCategoryTypeDto> query(SafeguardCategoryTypePageDto searchPageDto) {

		PageResult<SafeguardCategoryType> pageResult = safeguardCategoryTypeService
				.queryPageObj(searchPageDto.transToPageable());
		return new Page(change.changesToDTOPage(pageResult, SafeguardCategoryTypeDto.class));
	}

	@RequestMapping(value = Url.queryByProductId, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public List<SafeguardCategoryTypeDto> queryByProductId(@RequestParam("productId") Integer productId) {

		SafeguardCategoryType safeguardCategoryType = new SafeguardCategoryType();
		safeguardCategoryType.setProductId(productId);
		List<SafeguardCategoryType> list = safeguardCategoryTypeService.queryObj(safeguardCategoryType);
		return change.changesToDTOList(list, SafeguardCategoryTypeDto.class);
	}

}
